#ifndef __NIOCORE_X3G_SCALAR_H__
#define __NIOCORE_X3G_SCALAR_H__

#include <math.h>
#include "Fixed.h"

namespace pvmlib
{

#ifdef SCALAR_FIXEDPOINT
typedef Fixed scalar;
#define f2s(x)	(pvmlib::Fixed::f2x(x))
#define n2s(x)	(pvmlib::Fixed::n2x(x))
#define x2s(x)	(pvmlib::Fixed(x))
#define s2x(x)	(x)
#define sval(x)	((x).Value())

const scalar SCALAR_ZERO(0); 
const scalar SCALAR_ONE(Fixed::ONE); 
const scalar SCALAR_TWO(Fixed::TWO);
const scalar SCALAR_HALF(Fixed::HALF);
const scalar SCALAR_QUARTER(Fixed::HALF >> 1);
const scalar SCALAR_PI(Fixed::PI);
const scalar SCALAR_ONE_OVER_PI(Fixed::ONE_OVER_PI);
const scalar SCALAR_PI_OVER_2(Fixed::PI_OVER_2);
const scalar SCALAR_PI_OVER_4(Fixed::PI_OVER_4);
const scalar SCALAR_TWOPI(Fixed::TWOPI);
const scalar SCALAR_E(Fixed::E);
const scalar SCALAR_NAN(Fixed::FIXED_NAN);
const scalar SCALAR_ALMOST_ZERO(7);
const scalar SCALAR_MS(66);
const scalar SCALAR_MAX(Fixed::MAX_VAL)

#define sins(x) (pvmlib::Fixed::sinx((x)))
#define coss(x) (pvmlib::Fixed::cosx((x)))
#define tans(x) (pvmlib::Fixed::tanx((x)))
#define sqrts(x) (pvmlib::Fixed::sqrt((x)))
#define acoss(x) (pvmlib::Fixed::acosx((x)))
#define fabss(x) (pvmlib::Fixed::fabs((x)))
#define pows(x, y) (pvmlib::Fixed::pow((x), (y)))

#else

typedef float scalar;
#define f2s(x)	(x)
#define n2s(x)	((pvmlib::scalar)(x))
#define x2s(x)	(x/((float)(pvmlib::Fixed::ONE)))
#define s2x(x)	(pvmlib::Fixed::f2x(x))
#define sval(x)	(x)

const scalar SCALAR_ZERO = 0.0f; 
const scalar SCALAR_ONE = 1.0f; 
const scalar SCALAR_TWO = 2.0f;
const scalar SCALAR_HALF = 0.5f;
const scalar SCALAR_QUARTER = 0.25f;
const scalar SCALAR_PI = 3.1415926535897932384f;
const scalar SCALAR_ONE_OVER_PI = 1.0f / SCALAR_PI;
const scalar SCALAR_PI_OVER_2 = SCALAR_PI / 2.0f;
const scalar SCALAR_PI_OVER_3 = SCALAR_PI / 3.0f;
const scalar SCALAR_PI_OVER_4 = SCALAR_PI / 4.0f;
const scalar SCALAR_TWOPI = SCALAR_PI * 2.0f;
const scalar SCALAR_E = 2.7182818284590452354f;
const union {int i; scalar f;} iof = { 0x7fc00000 };
const scalar SCALAR_NAN = iof.f;
const scalar SCALAR_ALMOST_ZERO(0.0001f);
const scalar SCALAR_MS = 0.001f;
const scalar SCALAR_MAX = 3.402823466e+38f;

//#define sins(x) ((scalar)Fixed::sinx(Fixed::f2x(x)))
//#define coss(x) ((scalar)Fixed::cosx(Fixed::f2x(x)))
//#define tans(x) ((scalar)Fixed::tanx(Fixed::f2x(x)))
//#define sqrts(x) ((scalar)Fixed::sqrt(Fixed::f2x(x)))
//#define acoss(x) ((scalar)Fixed::acosx(Fixed::f2x(x)))

#if defined(_WIN32_WCE) || defined(__SYMBIAN32__)
#define sins(x) ((pvmlib::scalar)sin(x))
#define coss(x) ((pvmlib::scalar)cos(x))
#define tans(x) ((pvmlib::scalar)tan(x))
#define sqrts(x) ((pvmlib::scalar)sqrt(x))
#define acoss(x) ((pvmlib::scalar)acos(x))
#define pows(x, y) ((pvmlib::scalar)pow(x, y))
#else
#define sins(x) sinf(x)
#define coss(x) cosf(x)
#define tans(x) tanf(x)
#define sqrts(x) sqrtf(x)
#define acoss(x) acosf(x)
#define pows(x, y) powf(x, y)
#endif

FORCEINLINE scalar fabss(scalar x) 
{
	union {scalar f; int i;} foi = { x };
	foi.i &= 0x7fffffff;
	return foi.f;
}


#endif

#define mins(a, b) (a<b?a:b)
#define maxs(a, b) (a>b?a:b)
	
}

#endif //__NIOCORE_X3G_SCALAR_H__
